<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ CMS ]
 */
namespace Front\Controller;
use Think\Controller;

/**
 * 频道
 */
class IndexController extends Controller {

    /**
     * 首页
     */
    public function Index(){

        $this->display();
    }
    /**
     * 下载客户端
     */
    public function down_client(){

        $this->display();
    }
    /**
     * 关于我们
     */
    public function about_us(){

        $this->display();
    }
    /**
     * 关于我们
     */
    public function person_info(){
        $m_common_user = M('common_user');
        $m_common_user_country = M('common_user_country');
        $m_mall_content = M('mall_content');

        $id=getUID();

        if(IS_AJAX){
            $user=$m_common_user->find($id);
            if(empty($user)){
                $codes['errcode'] = 2;
                $codes['errmsg'] = '请先登录!';
                $this->ajaxReturn($codes);
            }
            if($user['group_id']==9){
                $codes['errcode'] = 2;
                $codes['errmsg'] = '你已经是音乐人，请不要重复申请!';
                $this->ajaxReturn($codes);
            }
            if($user['group_id']!=3){
                $codes['errcode'] = 2;
                $codes['errmsg'] = '你不是普通用户,不需要申请音乐人!';
                $this->ajaxReturn($codes);
            }
            if($user['group_id']==3&&$user['apply']==1){
                $codes['errcode'] = 2;
                $codes['errmsg'] = '你已经申请音乐人，请等待审核';
                $this->ajaxReturn($codes);
            }
            $data = I('');
            $style = I('param.style');
            $data['status'] = 1;
            $data['renling'] = 1;    //1未认领
            $data['apply'] = 1;    //带申请

            $result = $m_common_user->where("id={$id}")->save($data);

            if($result){
                $style=explode(',',$style);
                add_style(5,$id,$style);
            }
            $info = $result?get_errcode(0):get_errcode(10000004);
            $this->ajaxReturn($info);
        }else{
            //判断登录
            //获得需要生成的类型
            $type = getC('LOGIN_AUTH_TYPE');

            if($type['value']==1 and $_COOKIE['uid']==null){
                $this->error('请先登录',U('Front/Index/index'));
//                redirect(U('Front/Index/index'), 2, '请先登录。');
            }else if($type['value']==2 and $_SESSION['uid']==null){
                $this->error('请先登录',U('Front/Index/index'));
//                redirect(U('Front/Index/index'), 2, '请先登录。');
            }
            $country_list = $m_common_user_country->order("id asc")->select();

            #风格
            $style_maps['type'] = get_mall_model_conf('MALL_STYLE');
            $style_maps['status'] = 1;
            $style_list = $m_mall_content->where($style_maps)->order('sort desc')->select();
            $this->assign('style_list',$style_list);

            $this->assign('country_list',$country_list);



            $this->display();
        }

    }
   //注册
    public function register(){
        $m_common_user = M('common_user');
        $m_sms_code = M('sms_code');

        if(IS_AJAX){
            $data = I('');
            //判断验证码是否对
            $map['name'] = I('param.username');
            $map['code'] = I('param.code');

            $datas['status'] = 1;
            //判断是否能注册
            $result = $m_sms_code->where($map)->save($datas);
            if(!$result){
                $codes['errcode'] = 2;
                $codes['errmsg'] = '请输入正确的验证码!';
                $this->ajaxReturn($codes);

            }else{
                if(empty($data['password'])){
                    $codes['errcode'] = 2;
                    $codes['errmsg'] = '请输入密码！';
                    $this->ajaxReturn($codes);
                }
                $count=$m_common_user->where("username={$map['name']}")->count();
                if($count){
                    $codes['errcode'] = 2;
                    $codes['errmsg'] = '此用户已存在！';
                    $this->ajaxReturn($codes);
                }
                $data['password'] = md5(I("param.password")); //默认密码TODO动态配置
                $data['create_time'] = time();
                $data['create_ip'] = get_client_ip();
                $data['group_id'] = 3; //默认群组TODO动态配置
                $data['status'] = 1;
                $result = $m_common_user->add($data);
                //返回信息 TODO判断错误原因
                $info = $result?get_errcode(0):get_errcode(10000004);
                $this->ajaxReturn($info);
            }
        }
    }
    //登录
    public function login(){
        $m_common_user = M('common_user');
        $m_sms_code = M('sms_code');

        if(IS_AJAX){
            $data = I('');
            //判断验证码是否对
            $map['name'] = I('param.username');
            $map['code'] = I('param.code');

            $data['status'] = 1;
            //判断验证码
            $result = $m_sms_code->where($map)->save($data);
            if(!$result){

                $codes['errcode'] = 2;
                $codes['errmsg'] = '请输入正确的验证码！';
                $this->ajaxReturn($codes);

            }else{
                $map_user['tel'] = I('param.username');
                $udata = $m_common_user->where($map_user)->find();

                if($udata){

                    //统一登录接口
                    login($udata['id']);

                    //获得跳转着陆页
                    $url = getLoginURL($udata['id']);

                    action_log('admin_public_login');//行为记录

                    //跳转到着陆页
//                    $this->success('登录成功',U($url['url']));
                    $codes['errcode'] = 0;
                    $codes['errmsg'] = '登录成功!';
                    $this->ajaxReturn($codes);
                    //不是用户注册登录
                }
            }
        }
    }
   //忘记密码
    public function modify(){
        $m_common_user = M('common_user');
        $m_sms_code = M('sms_code');

        if(IS_AJAX){
            $data = I('');
            //判断验证码是否对
            $map['name'] = I('param.username');
            $map['code'] = I('param.code');

            $datass['status'] = 1;
            //判断是否有验证码
            $result = $m_sms_code->where($map)->save($datass);
            if(!$result){

                $codes['errcode'] = 2;
                $codes['errmsg'] = '请输入正确的验证码！';
                $this->ajaxReturn($codes);

            }else{
                if(empty($data['password'])){
                    $codes['errcode'] = 2;
                    $codes['errmsg'] = '请输入密码！';
                    $this->ajaxReturn($codes);
                }
                $telp["tel|username"]=$data['username'];
                $user=$m_common_user->where($telp)->find();
                if(empty($user)){
                    $codes['errcode'] = 2;
                    $codes['errmsg'] = '没有此用户！';
                    $this->ajaxReturn($codes);
                }
                if(!empty($user)){
                    $datas['password'] = md5(I("param.password")); //默认密码TODO动态配置
                    $result = $m_common_user->where("id={$user['id']}")->save($datas);
                }

                //返回信息 TODO判断错误原因
                $info = $result?get_errcode(0):get_errcode(10000004);
                $this->ajaxReturn($info);
            }
        }
    }


	
}